package com.carepad.util;

import java.text.SimpleDateFormat;
import java.util.Date;

public class DateConverterEnJa {
	
	//2013年11月6日 水曜日
	@SuppressWarnings("deprecation")
	public static String EnDatetoJa(Date date){
		
		StringBuilder sb = new StringBuilder();
		StringBuilder mnth = new StringBuilder();
		
		mnth.append(date.getMonth());
		
		int dayno = date.getDate();
		String dayOfWeek = getJaDayOfWeek(date.getDay());
		/*String month = getMonth(mnth.toString(),"ja");*/
		int month = date.getMonth() + 1;
		int year = date.getYear() + 1900;
		
		/*sb.append(dayOfWeek + ", " + month + "月 " + dayno + ", " + year);*/
		
		sb.append(year + "年" +  month + "月" + dayno + "日" + dayOfWeek) ;
		
		return sb.toString();
	}
	
	@SuppressWarnings("deprecation")
	public static String EnDatetoJaShort(Date date){
		
		StringBuilder sb = new StringBuilder();
		StringBuilder mnth = new StringBuilder();
		
		mnth.append(date.getMonth());
		
		int dayno = date.getDate();
		String dayOfWeek = getJaDayOfWeek(date.getDay());
		int month = date.getMonth() + 1;
		
		sb.append(month + "月" + dayno + "日" + dayOfWeek) ;
		
		return sb.toString();
	}
	
	public static String JaDatetoEnString(String jaDate){
		
/*		jaDate = "2013年11月6日 水曜日";*/
		Date enDate = JaDatetoEnDate(jaDate);		
	    SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd");
		
		return ft.format(enDate);
	}
	
	@SuppressWarnings("deprecation")
	public static Date JaDatetoEnDate(String jaDate){
		
/*		jaDate = "2013年11月6日 水曜日";*/
		Date enDate = new Date();
		int date;
		int month;
		int year;
		
		String[] dateparts = jaDate.split("年");	
		year = Integer.parseInt(dateparts[0].trim());
		
		String[] monthdate = dateparts[1].split("月");
		month = Integer.parseInt(monthdate[0].trim());
		
		String[] datestr = monthdate[1].split("日");
		date = Integer.parseInt(datestr[0].trim());		
		
		enDate.setYear(year - 1900);
		enDate.setMonth(month - 1);
		enDate.setDate(date);
	
		return enDate;
	}

	@SuppressWarnings("unused")
	private static String getMonth(String month, String flag){
	
		int i = 0;
		String[] mn = {"一","二","三","四","五","六","七","八","九","十","十一","十二"};
		StringBuilder sb = new StringBuilder();
		
        for (String a : mn) {
        	
        	StringBuilder stri = new StringBuilder();        	
        	stri.append(i);
        	
        	if(a.equals(month)){
	            if(flag.equals("en")){
	            	sb.append(i);
	            	break;
	            }
        	}
	        if(month.equals(stri.toString())){
	            	if(flag.equals("ja")){
	            	sb.append(a);
	            	break;
	            }
	        }
            i++;
        }
		
		return sb.toString();
	}
	
	
	private static String getJaDayOfWeek(int dow){
		
		String[] dy = {"日曜日","月曜日","火曜日","水曜日","木曜日","金曜日","土曜日"};

		return dy[dow];
	}
}
